package com.zhixuan.autoui.widget;

import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.zhixuan.autoui.databinding.DialogSpeechRecognitionBinding;

import cd.zhixuan.utils.GlobalTools;
import cd.zhixuan.utils.dialog.IDialog;

/**
 * @author: HuZC
 * @Description: 语音识别弹出框
 * @Date: 2022/1/10
 */
public class DialogSpeechRecognition extends IDialog {
    public static class Holder {
        static final DialogSpeechRecognition INSTANCE = new DialogSpeechRecognition(GlobalTools.getContext());
    }

    public static DialogSpeechRecognition getInstance() {
        return Holder.INSTANCE;
    }

    private DialogSpeechRecognitionBinding binding;

    private DialogSpeechRecognition(@NonNull Context context) {
        super(context);
    }

    @Nullable
    @Override
    public View layoutView() {
        binding = DialogSpeechRecognitionBinding.inflate(mLayoutInflater);
        return binding.getRoot();
    }

    @Override
    public int layoutRes() {
        return 0;
    }

    @Override
    public void initView() {

    }

    @Override
    protected void initWindowParams(@NonNull Window window, @NonNull WindowManager.LayoutParams lp, @NonNull WindowManager wm) {
        super.initWindowParams(window, lp, wm);

        lp.windowAnimations = android.R.style.Animation_Toast;
        lp.gravity = Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL;
        lp.y = (int) (0.05f * windowRect.height());
    }

    @Override
    protected void afterShowed(@NonNull Window window, @NonNull WindowManager.LayoutParams lp, @NonNull WindowManager wm) {
        super.afterShowed(window, lp, wm);
        lp.width = WindowManager.LayoutParams.WRAP_CONTENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;

    }

    /**
     * @param text 设置语音识别到的文本
     */
    public void setRecognitionText(String text) {
        try {
            binding.tvRecognition.setText(text);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
